<?php
// 本类由系统自动生成，仅供测试用途
namespace app\service\controller;

use app\common\controller\ServiceOauth;
use external\SFTools;

class Authorize extends ServiceOauth {

    public function initialize() {
        $this->get_check("json");
        $this->host_check("json");
    }

    /**
     * 接口Json格式输出内容
     * @param   string      appid       应用唯一标识
     * @param   string      secret      应用密钥
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getAccessToken() {
        $app_id = htmlspecialchars(input("get.appid"));
        $app_secret = htmlspecialchars(input("get.secret"));
        if (empty($app_id)) {
            exit(SFTools::pullJson(0, '3102'));
        }
        if (empty($app_secret)) {
            exit(SFTools::pullJson(0, '3104'));
        }
        $appLogic = new \backend\logic\AppLogic();
        $appFind = $appLogic->getAppFindBuyAppid($app_id);
        if (empty($appFind)) {
            exit(SFTools::pullJson(0, '3013'));
        }
        if ($appFind['app_secret'] != $app_secret) {
            exit(SFTools::pullJson(0, '3014'));
        }
        if ($appFind['app_status'] == 0) {
            exit(SFTools::pullJson(0, '3802'));
        }
        if ($appFind['attestation_status'] == 1) {
            exit(SFTools::pullJson(0, '3801'));
        }
        if ($appFind['attestation_status'] == 0) {
            exit(SFTools::pullJson(0, '3804'));
        }
        $access_token = SFTools::getUUID("U");
        $return_array['access_token'] = $access_token;
        $return_array['expires_in'] = 7200;
        SFTools::setCache($app_id, $return_array, 7260);
        return SFTools::pullJson(1, NULL, $return_array);
    }

    /**
     * 获取 32位字串
     * @param   string      f       唯一标识中英文是否为大小写 U 大写 / 其他都为小写
     * @return  string              32位字串
     */
    public function getUUIDStr() {
        $format = htmlspecialchars(input("get.f"));
        return SFTools::getUUID($format);
    }
}
